﻿using System.Web.Routing;

namespace System.Web.Mvc
{
	public static class RouteExtensions
	{
		public static Route AddDefault(this Route route, string name, object @default)
		{
			var defaults = new RouteValueDictionary {{name, @default}};
			return route.AddDefaults(defaults);
		}

		public static Route AddDefaults(this Route route, object defaults)
		{
			return route.AddDefaults(new RouteValueDictionary(defaults)); 
		}

		public static Route AddDefaults(this Route route, RouteValueDictionary defaults)
		{
			if (route.Defaults == null)
				route.Defaults = defaults;
			else
			{
				foreach (var defaultKey in defaults.Keys)
					route.Defaults[defaultKey] = defaults[defaultKey];
			}

			return route;
		}

		public static Route AddConstraint(this Route route, string name, object constraint)
		{
			var constraints = new RouteValueDictionary {{name, constraint}};
			return route.AddConstraints(constraints);
		}

		public static Route AddConstraints(this Route route, object constraints)
		{
			return route.AddConstraints(new RouteValueDictionary(constraints));
		}

		public static Route AddConstraints(this Route route, RouteValueDictionary constraints)
		{
			if (route.Constraints == null)
				route.Constraints = constraints;
			else
			{
				foreach (var constraintKey in constraints.Keys)
					route.Constraints[constraintKey] = constraints[constraintKey];
			}

			return route;
		}

		public static Route AddDataToken(this Route route, string name, object value)
		{
			var dataTokens = new RouteValueDictionary { { name, value } };
			return route.AddDataTokens(dataTokens);
		}

		public static Route AddDataTokens(this Route route, object dataTokens)
		{
			return route.AddDataTokens(new RouteValueDictionary(dataTokens));
		}

		public static Route AddDataTokens(this Route route, RouteValueDictionary dataTokens)
		{
			if (route.DataTokens == null)
				route.DataTokens = dataTokens;
			else
			{
				foreach (var dataTokenKey in dataTokens.Keys)
					route.DataTokens[dataTokenKey] = dataTokens[dataTokenKey];
			}

			return route;
		}
	}
}
